package com.atguigu.gulimall.search.thread;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author weichen
 * @email 519543138@qq.com
 * @date 2022-10-24 下午10:24
 */
public class ThreadTest {

    public static  ExecutorService executor = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main start---");
        /**
         * runAsync:无返回结果异步运行
         */
//        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(()->{
//            System.out.println("当前线程"+Thread.currentThread().getId());
//            int res=10/2;
//            System.out.println("执行结果"+res);
//        }, executor);

        /**
         * supplyAsync:有返回结果的异步运行
         * whenCompleteAsync，exceptionally方法完成后的感知
         */
//        CompletableFuture<Integer> supplyAsync = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程"+Thread.currentThread().getId());
//            int res=10/0;
//            System.out.println("执行结果"+res);
//            return res;
//        }, executor).whenCompleteAsync((re, throwable) -> {
//            //虽然能得到结果，但是无法修改返回数据
//            System.out.println("执行结果："+re+",异常:"+throwable);
//        },executor).exceptionally(throwable -> {
//            //感知异常，返回默认值
//            return 10;
//        });

        /**
         * handle 方法完成后的处理
         */
//        CompletableFuture<Integer> supplyAsync = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程"+Thread.currentThread().getId());
//            int res=10/0;
//            System.out.println("执行结果"+res);
//            return res;
//        }, executor).handle((res, throwable) ->{
//            return 0;
//        });

        /**
         * thenRunAsync,串行化，无返回结果
         */
//        CompletableFuture<Void> supplyAsync = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程"+Thread.currentThread().getId());
//            int res=10/4;
//            System.out.println("执行结果"+res);
//            return res;
//        }, executor).thenRunAsync(() -> {
//            System.out.println("执行方法2");
//        },executor);

        /**
         * thenAcceptAsync,接收方法1的结果，方法2无返回
         */
//        CompletableFuture<Void> supplyAsync = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程"+Thread.currentThread().getId());
//            int res=10/3;
//            System.out.println("执行结果"+res);
//            return res;
//        }, executor).thenAcceptAsync(integer -> {
//            System.out.println("执行方法2,，方法1结果："+integer);
//        },executor);


        /**
         * thenApplyAsync：接收方法1结果，返回方法2结果
         */
//        CompletableFuture<Integer> supplyAsync = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程"+Thread.currentThread().getId());
//            int res=10/3;
//            System.out.println("执行结果"+res);
//            return res;
//        }, executor).thenApplyAsync(integer -> {
//            System.out.println("执行方法2,，方法1结果："+integer);
//            return 1111;
//        },executor);
//        //get 是阻塞方法
//        int num=supplyAsync.get();
        /**
         * allOf：等待所有任务完成
         */
        CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("执行任务1");
            return "任务1";
        }, executor);
        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("执行任务2");
            return "任务2";
        }, executor);
        CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3000);
                System.out.println("执行任务3");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "任务3";
        }, executor);

        CompletableFuture<Void> allOf = CompletableFuture.allOf(future1, future2, future3);


//        System.out.println("main end---结果："+future1.get()+","+future2.get()+","+future3.get());
        System.out.println("main end");

    }



}
